package com.koron.css2.clientCenter.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.CtmInfoBean;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.clientCenter.queryBean.UserInfoQueryBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;

public class CmtInfoList implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            UserInfoQueryBean uiqBean = JsonUtils.objectToPojo(req.getData(), UserInfoQueryBean.class);

            UserInfoMapper uiMapper = factory.getMapper(UserInfoMapper.class);
            PageHelper.startPage(uiqBean.getPage(), uiqBean.getPageCount());
            // 获取客户列表
            List<CtmInfoBean> uiList = uiMapper.cmtInfoList(uiqBean);
            //客户信息
            Map<String, String> cdl = Tools.mapDicByCode(factory, "CDL");//信用等级
            Map<String, String> ctt = Tools.mapDicByCode(factory, "CTT");//客户类型
            Map<String, String> idt = Tools.mapDicByCode(factory, "IDT");//证件类型

            uiList.forEach(ctmInfoBean -> {
                ctmInfoBean.setCertTypeName(idt.get(ctmInfoBean.getCertType()));
                ctmInfoBean.setCreditLevelName(cdl.get(ctmInfoBean.getCreditLevel()));
                ctmInfoBean.setCtmTypeName(ctt.get(ctmInfoBean.getCtmType()));
            });

            @SuppressWarnings("rawtypes")
            MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", PageInfo.class);

            info.setData(new PageInfo(uiList));
            return info;
        } catch (Exception e) {
            logger.error("客户管理列表查询失败" + e.getMessage(), e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", null);
        }
    }
}
